﻿#include "datasocket.h"
#include "socketlibfunction.h"

SocketLib::DataSocket::DataSocket()
{
}

void SocketLib::DataSocket::writeBuffer(char* pBuffer, int aLen)
{
    m_sendBuffer.writeBuffer(pBuffer, aLen);
}

bool SocketLib::DataSocket::onRead()
{
    char localbuffer[4096];
    int    len;
    len = recv(m_fd, localbuffer, sizeof(localbuffer), 0);
    if (0 >= len)
    {
        return false;
    }

    onRecvComplete(localbuffer, len);
    return true;
}

bool SocketLib::DataSocket::onSend()
{
    bool sendret = true;

    if (m_sendBuffer.getUsableSize() > 0)
    {
        int transnum = SocketApi::SendData(m_fd, m_sendBuffer.getReadBuffer(), m_sendBuffer.getUsableSize());
        if (transnum >= 0)
        {
            m_sendBuffer.addReadPos(transnum);
        }
        else
        {
            sendret = false;
        }
    }

    return sendret;
}

void SocketLib::DataSocket::init()
{
    SocketFD::init();
    m_recvBuffer.init();
    m_sendBuffer.init();
}
